﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CNLTHD_GoogleMapAPI_DAO;
using CNLTHD_GoogleMapAPI_DTO;
using CNLTHD_GoogleMapAPI.UserControl;
using AjaxPro;

namespace CNLTHD_GoogleMapAPI
{
    public partial class MyMap : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(MyMap)); 
			if (!IsPostBack)
			{

				if (Request.QueryString["TenDiaDiem"] != null && Request.QueryString["MaDiaDiem"] != null)
				{
					string tenDiaDiem = Request.QueryString["TenDiaDiem"];
					string maDiaDiem = Request.QueryString["MaDiaDiem"];
					string text = Request.QueryString["text"];
				}
				if (Request.QueryString["action"] == "xoa")
					DiaDiemDAO.XoaDiaDiem(Int32.Parse(Request.QueryString["maDiaDiem"]));
				this.onLoad();
			}
        }

        public void onLoad()
        {


			string sTenDangNhap = (string)Session["TenDangNhap"];

			if (sTenDangNhap == null)
			{
				pnlThongBao.Visible = true;
				pnlMap.Visible = false;
				return;
			}
			else
			{
				pnlThongBao.Visible = false;
				pnlMap.Visible = true;
			}


			if (!IsPostBack)
			{
				List<DiaDiem> dsDiaDiem = DiaDiemDAO.LayDanhSachDiaDiem(sTenDangNhap);
				dsDiaDiem = dsDiaDiem.OrderBy(p => p.LoaiDiaDiem.MaLoaiDiaDiem).ToList();

                TreeNode root = new TreeNode();
				root.ImageUrl = ".\\img\\thumuc.png";
				root.Text = "Danh sách địa điểm của bạn";
				TreeViewMarker.Nodes.Add(root);

				int maLoaiDiaDiem = 0;
				int count = -1;
				foreach (DiaDiem idiadiem in dsDiaDiem)
				{

					if (idiadiem.LoaiDiaDiem.MaLoaiDiaDiem != maLoaiDiaDiem)
					{
						TreeNode sub = new TreeNode();
						sub.NavigateUrl = "javascript:KiemDiaDiemGanNhat('" + idiadiem.LoaiDiaDiem.MaLoaiDiaDiem + "')";
						sub.ImageUrl = ".\\img\\thumuc.png";
						sub.Text = idiadiem.LoaiDiaDiem.TenLoaiDiaDiem;
						TreeViewMarker.Nodes[0].ChildNodes.Add(sub);

						maLoaiDiaDiem = idiadiem.LoaiDiaDiem.MaLoaiDiaDiem;
						count++;
					}

					TreeNode subdd = new TreeNode();
					subdd.NavigateUrl = "javascript:findLocation('" + idiadiem.TenDiaDiem + "','" + false + "','" + idiadiem.MaDiaDiem + "')";
					subdd.ImageUrl = ".\\img\\diadiem.png";
					subdd.Text = idiadiem.TenDiaDiem;
					subdd.Value = idiadiem.MaDiaDiem.ToString();
					TreeViewMarker.Nodes[0].ChildNodes[count].ChildNodes.Add(subdd);
				}

				TreeNode rootNhan = new TreeNode();
				rootNhan.ImageUrl = ".\\img\\thumuc.png";
				rootNhan.Text = "Danh sách địa điểm nhận được";
				TreeViewMarker.Nodes.Add(rootNhan);

				List<ChiaSeDiaDiem> dsChiaSe = ChiaSeDiaDiemDAO.LayDanhSachChiaSeNguoiNhan(sTenDangNhap);
				dsChiaSe = dsChiaSe.OrderBy(ds => ds.DiaDiem.LoaiDiaDiem.MaLoaiDiaDiem).Distinct().ToList();

				maLoaiDiaDiem = 0;
				count = -1;
				foreach (ChiaSeDiaDiem chiaSe in dsChiaSe)
				{

					if (chiaSe.DiaDiem.LoaiDiaDiem.MaLoaiDiaDiem != maLoaiDiaDiem)
					{
						TreeNode sub = new TreeNode();
						sub.ImageUrl = ".\\img\\thumuc.png";
						sub.Text = chiaSe.DiaDiem.LoaiDiaDiem.TenLoaiDiaDiem;
						TreeViewMarker.Nodes[1].ChildNodes.Add(sub);

						maLoaiDiaDiem = chiaSe.DiaDiem.LoaiDiaDiem.MaLoaiDiaDiem;
						count++;
					}

					TreeNode subdd = new TreeNode();
					subdd.NavigateUrl = "javascript:findLocation('" + chiaSe.DiaDiem.TenDiaDiem + "','" + false + "','" + chiaSe.DiaDiem.MaDiaDiem + "')";
					subdd.ImageUrl = ".\\img\\diadiem.png";
					subdd.Text = chiaSe.DiaDiem.TenDiaDiem;
					subdd.Value = chiaSe.DiaDiem.MaDiaDiem.ToString();
					TreeViewMarker.Nodes[1].ChildNodes[count].ChildNodes.Add(subdd);
				}
            }
        }

        protected void TreeViewMarker_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                 TreeViewMarker.Attributes.Add("onclick", "return OnTreeClick(event)");
            }
        }

        [AjaxPro.AjaxMethod]
        public Object[] Hello(string pstrName)
        {
            string sTenDangNhap = (string)Session["TenDangNhap"];
            List<DiaDiem> dsDiaDiem = DiaDiemDAO.LayDanhSachDiaDiem(sTenDangNhap);
            Object[] tt = new Object[3];
            tt[0] = dsDiaDiem[0].KinhDo;
            tt[1] = dsDiaDiem[0].ViDo;
            tt[2] = dsDiaDiem[0].TenDiaDiem;
            return tt;
        }
    }
}